﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="FileSystemRepository.cs" company="Collaboris Ltd.">
//   Copyright (c) Collaboris Ltd. All rights Reserved.
// </copyright>
// <summary>
//   The file system report repository.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace Collaboris.Saf.Repositories
{
    using System;
    using System.IO;

    using Utils;

    /// <summary>
    /// The file system report repository. Stores and reads Macro reports from the file system
    /// </summary>
    /// <typeparam name="T">
    /// </typeparam>
    public abstract class FileRepository<T> : ISafRepository<T>
        where T : class
    {
        #region Enums

        /// <summary>
        /// The repository action.
        /// </summary>
        protected enum RepositoryAction
        {
            /// <summary>
            /// The load.
            /// </summary>
            load, 

            /// <summary>
            /// The save.
            /// </summary>
            save
        }

        #endregion

        #region Implemented Interfaces

        #region ISafRepository<T>

        /// <summary>
        /// Gets the report from a persistnace media.
        /// </summary>
        /// <param name="name">
        /// The name.
        /// </param>
        /// <returns>
        /// </returns>
        public T LoadObject(string name)
        {
            string fileName = this.GetFileName(name, RepositoryAction.load, null);

            if (File.Exists(fileName))
            {
                return SerialisationHelper.DeserializeFromFile<T>(fileName);
            }

            throw new FileNotFoundException(fileName);
        }

        /// <summary>
        /// Saves the report to a persistnace media.
        /// </summary>
        /// <param name="name">
        /// The name.
        /// </param>
        /// <param name="objectToSave">
        /// The object To Save.
        /// </param>
        public void SaveObject(string name, T objectToSave)
        {
            string fileName = this.GetFileName(name, RepositoryAction.save, objectToSave);

            if (String.IsNullOrEmpty(fileName))
            {
                throw new FileLoadException("Trying to save a file with an empty name");
            }

            SerialisationHelper.SerializeToFile<T>(objectToSave, fileName);
        }

        #endregion

        #endregion

        #region Helper Methods

        /// <summary>
        /// The get file name.
        /// </summary>
        /// <param name="name">
        /// The name.
        /// </param>
        /// <param name="action">
        /// The action.
        /// </param>
        /// <returns>
        /// The get file name.
        /// </returns>
        protected virtual string GetFileName(string name, RepositoryAction action, T objectToSave)
        {
            // By default we just return the filename that was provided.
            // If there is the need for a different name then just override this behaviour.
            return name;
        }

        #endregion
    }
}